package insertBook;

import libraryRun.MainFrame;
import mySQLServer.Link;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class InsertBook extends JFrame {
    private InsertBook insertBook;
    public InsertBook(){
        super("图书添加界面");
        insertBook=this;
    }

    public void insert(MainFrame myJFrame){
        setLayout(null);
        Container container=this.getContentPane();
        setSize(1200,650);


        //面板
        JPanel p1=new JPanel(new FlowLayout(FlowLayout.CENTER,0,35));
        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER,70,20));

        //文本行
        JTextField field1=new JTextField(15);
        JTextField field2=new JTextField(15);
        JTextField field3=new JTextField(15);
        JTextField field4=new JTextField(15);
        JTextField field5=new JTextField(15);
        JTextField field6=new JTextField(15);

        //按钮
        JButton button1=new JButton("添 加");
        JButton button2=new JButton("取 消");

        //监听器
        ImageIcon imageIcon1=new ImageIcon("icon/imageIcon6.jpg");
        imageIcon1.setImage(imageIcon1.getImage().getScaledInstance(50,45,Image.SCALE_AREA_AVERAGING));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(field1.getText().equals("")||field2.getText().equals("")||field3.getText().equals("")||field4.getText().equals("")||field5.getText().equals("")||field6.getText().equals("")){
                    JOptionPane.showMessageDialog(insertBook,"请输入完整图书信息","输入确认",1,imageIcon1);
                    return;
                }
                if(JOptionPane.showConfirmDialog(insertBook,"确认添加吗？","确认添加",JOptionPane.YES_NO_CANCEL_OPTION,1,imageIcon1)==0){
                    Link link=new Link();
                    try{
                        Connection con=link.linkDatebase();
                        String sql="Insert into [books] values ('"+field1.getText()+"',"+field2.getText()+",'"+field4.getText()+"',"+field3.getText()+",'"+field5.getText()+"',"+field6.getText()+","+field6.getText()+")";
                        String isExistedSql="select * from [books] where bookId="+field2.getText();
                        if(!insertIntoSqlServer(con,sql,isExistedSql))
                            JOptionPane.showMessageDialog(insertBook,"bookId已存在","信息匹配",1,imageIcon1);
                        else{
                            JOptionPane.showMessageDialog(insertBook,"添加成功","确认添加",1,imageIcon1);
                        }
                        link.closeDatebase(con);
                    }catch (ClassNotFoundException e1){
                        e1.printStackTrace();
                    }catch (SQLException e1){
                        e1.printStackTrace();
                    }
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                myJFrame.setVisible(true);
                dispose();
            }
        });

        insertBook.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {

            }

            public void windowClosing(WindowEvent e) {
                    myJFrame.setVisible(true);
            }

            public void windowClosed(WindowEvent e) {

            }

            public void windowIconified(WindowEvent e) {

            }

            public void windowDeiconified(WindowEvent e) {

            }

            public void windowActivated(WindowEvent e) {

            }

            public void windowDeactivated(WindowEvent e) {

            }
        });

        //标签
        JLabel label=new JLabel("图 书 添 加");
        JLabel label1=new JLabel("图 书 名");
        JLabel label2=new JLabel("图 书 Id");
        JLabel label3=new JLabel("价 格");
        JLabel label4=new JLabel("出 版 社");
        JLabel label5=new JLabel("作 者");
        JLabel label6=new JLabel("总 数 量");

        //字体
        Font font1=new Font("宋体",Font.BOLD,45);
        Font font2=new Font("宋体",Font.BOLD,30);
        Font font3=new Font("宋体",Font.BOLD,17);

        //设置字体
        label.setFont(font1);
        label1.setFont(font2);
        label2.setFont(font2);
        label3.setFont(font2);
        label4.setFont(font2);
        label5.setFont(font2);
        label6.setFont(font2);
        field1.setFont(font3);
        field2.setFont(font3);
        field3.setFont(font3);
        field4.setFont(font3);
        field5.setFont(font3);
        field6.setFont(font3);

        //设置大小
        label1.setPreferredSize(new Dimension(180,30));
        label2.setPreferredSize(new Dimension(180,30));
        label3.setPreferredSize(new Dimension(180,30));
        label4.setPreferredSize(new Dimension(180,30));
        label5.setPreferredSize(new Dimension(180,30));
        label6.setPreferredSize(new Dimension(180,30));
        button1.setPreferredSize(new Dimension(100,30));
        button2.setPreferredSize(new Dimension(100,30));

        //设置位置
        p1.setBounds(220,20,750,120);
        p2.setBounds(360,150,500,400);

        //放入面板
        p1.add(label);

        p2.add(label1);
        p2.add(field1);
        p2.add(label2);
        p2.add(field2);
        p2.add(label3);
        p2.add(field3);
        p2.add(label4);
        p2.add(field4);
        p2.add(label5);
        p2.add(field5);
        p2.add(label6);
        p2.add(field6);
        p2.add(button1);
        p2.add(button2);

        //放入容器
        container.add(p1);
        container.add(p2);

        setIconImage((new ImageIcon("icon/imageIcon5.jpg")).getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public boolean insertIntoSqlServer(Connection con,String sql,String isExistedSql) throws SQLException {
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery(isExistedSql);
        while (rs.next()){
            return false;
        }
        stmt.executeUpdate(sql);
        return true;
    }

    
}
